package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


import com.itcast.donkey.R;
import com.itcast.donkey.activity.TravelInHeadImageActivity;
import com.itcast.donkey.activity.TravelInItemActiity;
import com.itcast.donkey.modles.TravelItemModels;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/8/31.
 */
public class TravelItemAdapter extends RecyclerView.Adapter<TravelItemAdapter.ItemViewHolder> {
    private Context context;
    private List<TravelItemModels> travelItemModels = new ArrayList<>();

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public TravelItemAdapter(Context context, List<TravelItemModels> travelItemModels) {
        this.context = context;
        this.travelItemModels = travelItemModels;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_travel, null);
        ItemViewHolder holder = new ItemViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        final int i = position;
        Picasso.with(context).load(travelItemModels.get(i).getFront_cover_photo_url()).into(itemViewHolder.ivTravelBackground);
        itemViewHolder.ivTravelBackground.setScaleType(ImageView.ScaleType.FIT_XY);
        itemViewHolder.tvTravelName.setText(travelItemModels.get(i).getName());
        itemViewHolder.tvTravelDate.setText(travelItemModels.get(i).getStart_date());
        itemViewHolder.tvTravelDays.setText(travelItemModels.get(i).getDays() + "天");
        itemViewHolder.tvTravelPhotosCount.setText(travelItemModels.get(i).getPhotos_count() + "图");
        Picasso.with(context).load(travelItemModels.get(i).getUser().getImage()).into(itemViewHolder.ibTravelImage);

        //Item图片点击事件，带值跳转
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = travelItemModels.get(position).getId();
                String s = Integer.toString(id);
                Intent intent = new Intent(context, TravelInItemActiity.class);
                intent.putExtra("id", s);
                context.startActivity(intent);
            }
        });

        sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
        editor = sp.edit();

        //头像点击事件，带值跳转
        holder.ibTravelImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int headId = travelItemModels.get(position).getUser().getId();
                String hID = Integer.toString(headId);
                Intent intent = new Intent(context, TravelInHeadImageActivity.class);
                intent.putExtra("headID", hID);
                //游记的id和喜欢的ID
                editor.putString("headimage", hID);
                //提交
                editor.commit();
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return travelItemModels.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivTravelBackground;
        private TextView tvTravelName;
        private TextView tvTravelDate;
        private TextView tvTravelDays;
        private TextView tvTravelPhotosCount;
        private ImageButton ibTravelImage;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ivTravelBackground = (ImageView) itemView.findViewById(R.id.iv_travel_background);
            tvTravelName = (TextView) itemView.findViewById(R.id.tv_travel_name);
            tvTravelDate = (TextView) itemView.findViewById(R.id.tv_travel_date);
            tvTravelDays = (TextView) itemView.findViewById(R.id.tv_travel_days);
            tvTravelPhotosCount = (TextView) itemView.findViewById(R.id.tv_travel_photos_count);
            ibTravelImage = (ImageButton) itemView.findViewById(R.id.ib_travel_image);
        }
    }
}
